package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.domain.TaskRouse;
import com.lebang.example.AndroidVersionSdkErrorExample;
import com.lebang.model.MemberSdkErrorModel;
import com.lebang.service.AndroidSdkErrorService;
import com.lebang.service.NewRouseService;
import com.lebangweb.common.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;


/**
 * 手机信号数据源错误码统计结果
 *
 * @author xwj
 * @date 2020-12-18
 */
@Controller
@RequestMapping("/sys/androidsdkerror")
public class AndroidSdkErrorController {
    @Autowired
    private AndroidSdkErrorService baseService;

    @Autowired
    private NewRouseService newRouseService;

    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;

    @Login
    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        return "/androidsdkerror/list";
    }


    /**
     * @param page   页数
     * @param limit  条数
     * @param star   开始日期
     * @param end    结束日期
     * @param sdkKey 数据源
     * @return 返回数据
     */
    @PostMapping("/page")
    @ResponseBody
    @Login
    public HashMap newList(Long page, Integer limit, String star, String end, String sdkKey, HttpSession session,String errorCode) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        HashMap map = new HashMap(1);
        AndroidVersionSdkErrorExample example = new AndroidVersionSdkErrorExample();
        AndroidVersionSdkErrorExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("create_date desc,num desc");
        if (!TextUtil.isEmpty(star, end)) {
            //不为空
            LocalDate starDate = LocalDate.parse(star, fmt);
            LocalDate endDate = LocalDate.parse(end, fmt);
            c.andCreateDateBetween(starDate, endDate);
        }
        if (!TextUtil.isEmpty(sdkKey)) {
            c.andSdkKeyEqualTo(sdkKey);
        }

        if(!TextUtil.isEmpty(errorCode)){
            c.andErrorCodeEqualTo(errorCode);
        }

        if(newRouseService.isRoot(session)){
            c.andSdkKeyNotIn(sdkList);
        }

        map.put("count", this.baseService.countByExample(example));
        map.put("code", 0);

        List<MemberSdkErrorModel> masters = baseService.selectByExample(example);
        map.put("data", masters);
        return map;
    }


    /**
     * 统计指定日期新手任务
     *
     * @param date 日期
     * @return 返回结果
     */
    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen(String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end = LocalDate.parse(date, fmt);
        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN));
        return "success";
    }


}
